package com.google.android.volley.elegant;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.scheme.HostNameResolver;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.android.volley.VolleyLog;


public class ElegantPlainSocketFactory implements SocketFactory {

    public ElegantPlainSocketFactory()
    {
        this(null);
    }

    public ElegantPlainSocketFactory(HostNameResolver hostnameresolver)
    {
        nameResolver = hostnameresolver;
    }

    public static ElegantPlainSocketFactory getSocketFactory()
    {
        return DEFAULT_FACTORY;
    }

    public Socket connectSocket(Socket socket, String s, int i, InetAddress inetaddress, int j, HttpParams httpparams)
        throws IOException
    {
        if(s == null)
            throw new IllegalArgumentException("Target host may not be null.");
        if(httpparams == null)
            throw new IllegalArgumentException("Parameters may not be null.");
        if(socket == null)
            socket = createSocket();
        if(inetaddress != null || j > 0)
        {
            if(j < 0)
                j = 0;
            socket.bind(new InetSocketAddress(inetaddress, j));
        }
        int k = HttpConnectionParams.getConnectionTimeout(httpparams);
        InetSocketAddress inetsocketaddress;
        if(nameResolver != null)
            inetsocketaddress = new InetSocketAddress(nameResolver.resolve(s), i);
        else
            inetsocketaddress = new InetSocketAddress(s, i);
        try
        {
            long l = System.currentTimeMillis();
            socket.connect(inetsocketaddress, k);
            long l1 = System.currentTimeMillis() - l;
            if(VolleyLog.DEBUG)
            {
                VolleyLog.d("Established connection to [host=%s] in [%s ms]", s,l1);
            }
        }
        catch(SocketTimeoutException sockettimeoutexception)
        {
            throw new ConnectTimeoutException((new StringBuilder()).append("Connect to ").append(inetsocketaddress).append(" timed out").toString());
        }
        return socket;
    }

    public Socket createSocket()
    {
        return new Socket();
    }

    public boolean equals(Object obj)
    {
        boolean flag;
        if(obj == this)
            flag = true;
        else
            flag = false;
        return flag;
    }

    public int hashCode()
    {
        return ElegantPlainSocketFactory.class.hashCode();
    }

    public final boolean isSecure(Socket socket)
        throws IllegalArgumentException
    {
        if (socket == null)
            throw new IllegalArgumentException("Socket may not be null.");
          if (socket.getClass() != Socket.class)
            throw new IllegalArgumentException("Socket not created by this factory.");
          if (socket.isClosed())
            throw new IllegalArgumentException("Socket is closed.");
          return false;
    }

    private static final ElegantPlainSocketFactory DEFAULT_FACTORY = new ElegantPlainSocketFactory();
    private final HostNameResolver nameResolver;

}
